﻿namespace ST_CSharp_扩展方法
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //LinqDemo();
            //WordCount();
            CheckPassing();
        }

        /// <summary>
        /// Linq中的扩展方法
        /// </summary>
        private static void LinqDemo()
        {
            int[] ints = { 10, 45, 38, 13, 7, 20, 39, 23 };
            var result = ints.OrderBy(g => g);
            foreach (var i in result)
            {
                Console.WriteLine(i);
            }
        }

        /// <summary>
        /// 普通扩展方法
        /// </summary>
        private static void WordCount()
        {
            string s = "My name is ZhangShijie,I like .Net and CSharp very much!";
            Console.WriteLine($"Word count: {s.WordCount()}");
        }

        /// <summary>
        /// 枚举中的扩展方法
        /// </summary>
        private static void CheckPassing()
        {
            Console.WriteLine(Grades.A.Passing());
            Console.WriteLine(Grades.D.Passing());
            Console.WriteLine(Grades.F.Passing());
        }
    }

    public enum Grades { F = 0, D = 1, C = 2, B = 3, A = 4 }

    public static class StringExtension
    {
        public static int WordCount(this string str)
        {
            return str.Split(new char[] { ' ', ',', '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }

    public static class EnumExtension
    {
        public static Grades minPassing = Grades.D;
        public static bool Passing(this Grades grade)
        {
            return grade >= minPassing;
        }
    }
}